﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Krilib.Control.MessageObjects
{
    public class Image : MessageObject
    {
        System.Drawing.Image cache = null;

        public Image(Uri uri)
        {
            ImageUri = uri;
        }

        Uri _imgUri;
        public Uri ImageUri
        {
            get { return _imgUri; }
            set
            {
                _imgUri = value;
                Reset();
            }
        }

        public void RefreshImage()
        {
            Reset();
            cache = Properties.Resources.loading;
            Bridge.ImageManager.GetImageCallback(ImageUri, false, new Krilib.Bridges.GetImageCallbackDelegate(ImageDLCompleted));
        }

        public void ImageDLCompleted(Uri uri, System.Drawing.Image img)
        {
            Reset();
            if (ImageUri == uri && img != null)
                cache = img;
            else
                cache = Properties.Resources.error;
            Bridge.MainForm.RefreshTL(true);
        }

        /// <summary>
        /// Use static size prop
        /// </summary>
        public bool UseStaticSize { get; set; }

        /// <summary>
        /// Static size for rendering image
        /// </summary>
        public Size StaticSize { get; set; }

        /// <summary>
        /// No line-break before and after rendering
        /// </summary>
        public bool Inline { get; set; }

        public override void Draw(Graphics g, Point location)
        {
            //not render image in single-line mode
            Rectangle destination = dest;
            destination.X += location.X;
            destination.Y += location.Y;
            g.DrawImage(cache, destination);
            realDest = destination;
        }

        Rectangle cRect = new Rectangle();
        Rectangle dest = new Rectangle();
        Rectangle realDest = new Rectangle();
        public override IEnumerable<Rectangle> Measure(Rectangle target, int initHeight)
        {
            cRect = target;
            if (!Inline)
                Measuring.NewLine(ref cRect);
            if (UseStaticSize)
            {
                dest = new Rectangle(cRect.Location, StaticSize);
            }
            else
            {
                System.Drawing.Image cImg = null;
                if (cache == null)
                    RefreshImage();
                cImg = cache;
                dest = new Rectangle(cRect.Location, cImg.Size);
            }
            cRect.Height = dest.Height;
            if (!Inline)
                Measuring.NewLine(ref cRect);
            yield return dest;
        }

        public override Rectangle MeasureEndRect
        {
            get { return cRect; }
        }

        public override void Reset()
        {
            if (cache != null)
            {
                cache.Dispose();
                cache = null;
            }
        }

        public override IEnumerable<Rectangle> ItemRect
        {
            get { yield return realDest; }
        }

        Dialogs.ImageViewer iv = null;
        public override void OnMouseClick(object sender, Krilib.Control.Base.ChainedMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                iv = new Krilib.Control.Dialogs.ImageViewer();
                iv.Left = e.X - (iv.Height / 2);
                iv.Top = e.Y - (iv.Width / 2);
                iv.imageUri = ImageUri;
                iv.Show();
            }
        }
    }
}
